package com.itheima.imclient100.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import com.itheima.imclient100.BaseActivity;
import com.itheima.imclient100.MainActivity;
import com.itheima.imclient100.R;
import com.itheima.imclient100.presenter.SplashPresenter;
import com.itheima.imclient100.presenter.impl.SplashPresenterImpl;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class SplashActivity extends BaseActivity implements SplashView {
    @InjectView(R.id.iv_image)
    ImageView ivImage;
    private SplashPresenter mSplashPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ButterKnife.inject(this);
        mSplashPresenter = new SplashPresenterImpl(this);
        //获取登录的状态
        mSplashPresenter.getLoginState();
    }

    @Override
    public void onGetLoginState(boolean isLoggedInBefore) {
        if (isLoggedInBefore) {
            startActivity(new Intent(getApplicationContext(), MainActivity.class));
            finish();
        } else {
            //给imageView添加动画 动画执行结束的时候跳转登录页面
            ObjectAnimator alpha = ObjectAnimator.ofFloat(ivImage, "alpha", 0, 1);
            alpha.setDuration(2000);
            alpha.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    //当动画执行结束的时候会走这个方法
                    startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                    finish();
                }
            });
            alpha.start();
        }
    }
}
